﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:TaxAuditAdjust
    /// </summary>
    public partial class TaxAuditAdjust
    {
        public TaxAuditAdjust()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.TaxAuditAdjust GetModel(decimal FTaxAuditAdjustID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FTaxAuditAdjustID,FEntityNo,FYear,FTaxClassAllowanceNo,FAdjustAmount,FAdjustDate,RKRQ,FNote,FAdjustDeptNo,FAdjustEmpNo,FAdjustIP,FStatus,FTaxAuditId from fund.t_TaxAuditAdjust ");
            strSql.Append(" where FTaxAuditAdjustID=@FTaxAuditAdjustID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaxAuditAdjustID", SqlDbType.Decimal)
			};
            parameters[0].Value = FTaxAuditAdjustID;

            EnterpriseFund.Model.TaxAuditAdjust model = new EnterpriseFund.Model.TaxAuditAdjust();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.TaxAuditAdjust DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.TaxAuditAdjust model = new EnterpriseFund.Model.TaxAuditAdjust();
            if (row != null)
            {
                if (row["FTaxAuditAdjustID"] != null && row["FTaxAuditAdjustID"].ToString() != "")
                {
                    model.FTaxAuditAdjustID = row["FTaxAuditAdjustID"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FTaxClassAllowanceNo"] != null)
                {
                    model.FTaxClassAllowanceNo = row["FTaxClassAllowanceNo"].ToString();
                }
                if (row["FAdjustAmount"] != null && row["FAdjustAmount"].ToString() != "")
                {
                    model.FAdjustAmount = decimal.Parse(row["FAdjustAmount"].ToString());
                }
                if (row["FAdjustDate"] != null && row["FAdjustDate"].ToString() != "")
                {
                    model.FAdjustDate = DateTime.Parse(row["FAdjustDate"].ToString());
                }
                if (row["RKRQ"] != null && row["RKRQ"].ToString() != "")
                {
                    model.RKRQ = DateTime.Parse(row["RKRQ"].ToString());
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FAdjustDeptNo"] != null)
                {
                    model.FAdjustDeptNo = row["FAdjustDeptNo"].ToString();
                }
                if (row["FAdjustEmpNo"] != null)
                {
                    model.FAdjustEmpNo = row["FAdjustEmpNo"].ToString();
                }
                if (row["FAdjustIP"] != null)
                {
                    model.FAdjustIP = row["FAdjustIP"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FTaxAuditId"] != null)
                {
                    model.FTaxAuditId = row["FTaxAuditId"].ToString();
                }
                if (row.Table.Columns.Contains("FEmpName"))
                {
                    if (row["FEmpName"] != null)
                    {
                        model.FEmpName = row["FEmpName"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FTaxClassAllowanceName"))
                {
                    if (row["FTaxClassAllowanceName"] != null)
                    {
                        model.FTaxClassAllowanceName = row["FTaxClassAllowanceName"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("NSRSBH"))
                {
                    if (row["NSRSBH"] != null)
                    {
                        model.NSRSBH = row["NSRSBH"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("NSRBM"))
                {
                    if (row["NSRBM"] != null)
                    {
                        model.NSRBM = row["NSRBM"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("NSRMC"))
                {
                    if (row["NSRMC"] != null)
                    {
                        model.NSRMC = row["NSRMC"].ToString();
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select FTaxAuditAdjustID,a.FEntityNo,a.FYear,a.FTaxClassAllowanceNo,FAdjustAmount,FAdjustDate
,RKRQ,FNote,FAdjustDeptNo,FAdjustEmpNo,FAdjustIP,FStatus,FTaxAuditId 
,c.FEmpName,b.FTaxClassAllowanceName");
            strSql.Append(@" FROM fund.t_TaxAuditAdjust a LEFT JOIN fund.t_TaxClassAllowance b ON a.FTaxClassAllowanceNo = b.FTaxClassAllowanceNo AND a.FYear = b.FYear
 LEFT JOIN dbo.v_HREmpl c ON a.FAdjustEmpNo=c.FEmplNo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetLastAdjustList(string nsrdzdah)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select b.* from fund.t_TaxAuditForm a inner join fund.t_TaxAuditAdjust b
on a.FTaxAuditId=b.FTaxAuditId
where FInsDate=(
select MAX(FInsDate) from fund.t_TaxAuditForm
where NSRDZDAH='" + nsrdzdah + @"')
and NSRDZDAH='" + nsrdzdah + @"'");
            return DbHelperSQL.Query(strSql.ToString());
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetFAdjustEmp()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT DISTINCT FAdjustEmpNo,b.FEmpName FROM fund.t_TaxAuditAdjust a LEFT JOIN dbo.t_HREmpl b ON a.FAdjustEmpNo=b.FEmplNo");
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetQueryList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  select * from fund.t_TaxAuditAdjust left join t_HREmpl on t_HREmpl.FEntityNo=fund.t_TaxAuditAdjust.FEntityNo and FAdjustEmpNo=FEmplNo left join   (SELECT FTaxClassAllowanceNo,FTaxClassAllowanceName FROM  fund.t_TaxClassAllowance GROUP BY  FTaxClassAllowanceNo,FTaxClassAllowanceName) as t1 on fund.t_TaxAuditAdjust.FTaxClassAllowanceNo=t1.FTaxClassAllowanceNo join (select NSRSBH,NSRBM,fund.t_TaxAuditForm.NSRMC,fund.t_TaxAuditForm.FTaxAuditId from fund.t_TaxAuditForm left join  ltais.DJ_NSRXX on fund.t_TaxAuditForm.NSRDZDAH =ltais.DJ_NSRXX.NSRDZDAH)as t2 on t2.FTaxAuditId=fund.t_TaxAuditAdjust.FTaxAuditId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListForYearDataShow(string NSRDZDAH,string fYear)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"     SELECT a.FTaxClassAllowanceNo,a.FYear,a.FAdjustAmount,b.FTaxClassAllowanceName FROM (
                                        SELECT FTaxClassAllowanceNo,FYear,SUM(FAdjustAmount) AS FAdjustAmount FROM 
                                        fund.v_TaxAuditEntry WHERE FTaxAuditId IN (SELECT FTaxAuditId FROM fund.t_TaxAuditForm WHERE NSRDZDAH='" + NSRDZDAH + @"') AND FYear='" + fYear + @"'
                                        GROUP BY FTaxClassAllowanceNo,FYear)AS a 
                                        LEFT  JOIN fund.t_TaxClassAllowance b ON 
                                        a.FTaxClassAllowanceNo = b.FTaxClassAllowanceNo 
                                        AND a.FYear = b.FYear");
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllDateListByYear(string fYear)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"     SELECT a.FTaxClassAllowanceNo,a.FYear,a.FAdjustAmount,b.FTaxClassAllowanceName,a.NSRDZDAH FROM (
                                        SELECT NSRDZDAH,FTaxClassAllowanceNo,d.FYear,SUM(FAdjustAmount) AS FAdjustAmount FROM 
                                        fund.v_TaxAuditEntry AS d LEFT JOIN fund.t_TaxAuditForm e ON d.FTaxAuditId = e.FTaxAuditId   WHERE d.FTaxAuditId IN (SELECT FTaxAuditId FROM fund.t_TaxAuditForm ) AND d.FYear='" + fYear + @"'
                                        GROUP BY NSRDZDAH,FTaxClassAllowanceNo,d.FYear)AS a 
                                        LEFT  JOIN fund.t_TaxClassAllowance b ON 
                                        a.FTaxClassAllowanceNo = b.FTaxClassAllowanceNo 
                                        AND a.FYear = b.FYear");
            return DbHelperSQL.Query(strSql.ToString());
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListForPolicy(string FTaxAuditId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  SELECT a.FTaxClassAllowanceNo,a.FYear,a.FAdjustAmount as se,b.FTaxClassAllowanceName FROM (
                                        SELECT FTaxClassAllowanceNo,FYear,SUM(FAdjustAmount) AS FAdjustAmount FROM fund.v_TaxAuditEntry 
                                        WHERE FTaxAuditId='" + FTaxAuditId + @"'
                                        GROUP BY FTaxClassAllowanceNo,FYear) AS a 
                                        LEFT  JOIN fund.t_TaxClassAllowance b ON 
                                        a.FTaxClassAllowanceNo = b.FTaxClassAllowanceNo 
                                        AND a.FYear = b.FYear");
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListForYearDataShowEntry(string NSRDZDAH, string fYear)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"     SELECT a.FTaxClassAllowanceNo,a.FYear,a.FAdjustAmount,b.FTaxClassAllowanceName,dataType,FAdjustDate,FNote,FAdjustEmpNo FROM (
                                        SELECT FTaxClassAllowanceNo,FYear,FAdjustAmount,dataType,FAdjustDate,FNote,FAdjustEmpNo   FROM 
                                        fund.v_TaxAuditEntry WHERE FTaxAuditId IN (SELECT FTaxAuditId FROM fund.t_TaxAuditForm WHERE NSRDZDAH='" + NSRDZDAH + @"') AND FYear='" + fYear + @"'
                                        ) AS a 
                                        LEFT  JOIN fund.t_TaxClassAllowance b ON 
                                        a.FTaxClassAllowanceNo = b.FTaxClassAllowanceNo 
                                        AND a.FYear = b.FYear");
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListForPolicyEntry(string FTaxAuditId, string FTaxClassAllowanceNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  SELECT a.FTaxClassAllowanceNo,a.FYear,a.FAdjustAmount,b.FTaxClassAllowanceName,dataType,FAdjustDate,FNote,FAdjustEmpNo FROM (
                                        SELECT FTaxClassAllowanceNo,FYear,FAdjustAmount,dataType,FAdjustDate,FNote,FAdjustEmpNo  FROM fund.v_TaxAuditEntry 
                                        WHERE FTaxAuditId='" + FTaxAuditId + @"' AND FTaxClassAllowanceNo='" + FTaxClassAllowanceNo + @"') AS a 
                                        LEFT  JOIN fund.t_TaxClassAllowance b ON 
                                        a.FTaxClassAllowanceNo = b.FTaxClassAllowanceNo 
                                        AND a.FYear = b.FYear");
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FTaxAuditAdjustID,FEntityNo,FYear,FTaxClassAllowanceNo,FAdjustAmount,FAdjustDate,RKRQ,FNote,FAdjustDeptNo,FAdjustEmpNo,FAdjustIP,FStatus,FTaxAuditId ");
            strSql.Append(" FROM fund.t_TaxAuditAdjust ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fund.t_TaxAuditAdjust ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FTaxAuditAdjustID desc");
            }
            strSql.Append(")AS Row, T.*  from fund.t_TaxAuditAdjust T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_TaxAuditAdjust";
            parameters[1].Value = "FTaxAuditAdjustID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

